//
//  QuickOrderTableController.m
//  Futures
//
//  Created by Cheng on 2017/6/27.
//  Copyright © 2017年 Cheng. All rights reserved.
//

#import "QuickOrderTableController.h"
#import "QuickOrderModel.h"

@interface QuickOrderTableController ()<UITableViewDelegate>{
    UIButton *_preCountBtn;
    UIButton *_prePriceBtn;
}
@property (weak, nonatomic) IBOutlet UIView *footerView;

@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;

@property (weak, nonatomic) IBOutlet UIButton *one;
@property (weak, nonatomic) IBOutlet UIButton *two;
@property (weak, nonatomic) IBOutlet UIButton *three;
@property (weak, nonatomic) IBOutlet UIButton *four;
@property (weak, nonatomic) IBOutlet UIButton *five;

@property (weak, nonatomic) IBOutlet UIButton *onePrice;
@property (weak, nonatomic) IBOutlet UIButton *twoPrice;
@property (weak, nonatomic) IBOutlet UIButton *threePrice;

@property (weak, nonatomic) IBOutlet UIButton *startQuickBtn;

@end

@implementation QuickOrderTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"美原油CL1708";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highlightedIcon:nil target:self action:@selector(back)];
    
    // 调整footer的高度
    CGRect frame = self.footerView.frame;
    frame.size.height = SCREEN_HEIGHT - 50*7-64;
    self.footerView.frame = frame;
    
    self.agreeBtn.selected = YES;
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


- (IBAction)clickOrder:(id)sender {
    UIButton *btn = sender;
    MYLog(@"%@",btn.currentTitle);
    btn.backgroundColor = UICOLOR_RGB_Alpha(0x798579, 1);
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if (_preCountBtn&&btn!= _preCountBtn) {
        _preCountBtn.backgroundColor = [UIColor blackColor];
        [_preCountBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    _preCountBtn = btn;
}

- (IBAction)clickPriceBtn:(id)sender {
    UIButton *btn = sender;
    MYLog(@"%@",btn.currentTitle);
    btn.backgroundColor = UICOLOR_RGB_Alpha(0x798579, 1);
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if (_prePriceBtn&&btn!= _prePriceBtn) {
        _prePriceBtn.backgroundColor = [UIColor blackColor];
        [_prePriceBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    _prePriceBtn = btn;
}
- (IBAction)clickBuy:(id)sender {
    
    QuickOrderModel *model = [[QuickOrderModel alloc] init];
    model.qty = (int)_preCountBtn.tag;
    model.price = [_prePriceBtn.currentTitle floatValue];
    [model archiveRootObjecttoFileName:GLOBAL_INSTANCE.quoteModel.Symbolname];
}

- (IBAction)clickAgree:(id)sender {
    [sender setSelected:![sender isSelected]];
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    QuickOrderModel *model = [[QuickOrderModel alloc] unarchiveObjectWithFileName:GLOBAL_INSTANCE.quoteModel.Symbolname];
    MYLog(@"%@",model.properties_aps);
    if (model) {
        if (model.qty == self.one.tag) {
            [self clickOrder:self.one];
            [self setButtonWithButton:self.one];
        }else if (model.qty == self.two.tag) {
            [self clickOrder:self.two];
            [self setButtonWithButton:self.two];
        }else if (model.qty == self.three.tag) {
            [self clickOrder:_three];
            [self setButtonWithButton:self.three];
        }else if (model.qty == self.four.tag) {
            [self clickOrder:self.four];
            [self setButtonWithButton:self.four];
        }else if (model.qty == self.five.tag) {
            [self clickOrder:self.five];
            [self setButtonWithButton:self.five];
        }
        
        if (model.price == [self.onePrice.currentTitle floatValue]) {
            [self clickOrder:self.onePrice];
        }else if (model.price == [self.twoPrice.currentTitle floatValue]) {
            [self clickOrder:self.twoPrice];
        }else if (model.price == [self.threePrice.currentTitle floatValue]) {
            [self clickOrder:self.threePrice];
        }
        
        [self.startQuickBtn setTitle:@" 已开启" forState:UIControlStateNormal];
    }else{
        [self.startQuickBtn setTitle:@" 请开启" forState:UIControlStateNormal];
    }
    
}

- (void)setButtonWithButton:(UIButton *)btn{
    btn.backgroundColor = UICOLOR_RGB_Alpha(0x798579, 1);
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

@end
